/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package graph;

import java.awt.Color;
import java.io.Serializable;
import java.util.ArrayList;

/**
 *
 * @author borysfan
 */
public class Node implements Comparable<Node>, Serializable{
    private int id;
    private String name;
    private ArrayList<Edge> connections;
    private ArrayList<NodeEvent> events;
    private boolean lock = false;
    private Color color = Color.black;
    private Color defcolor = Color.black;



    /*zmienna pomocnicze do algorytmu Dijkstry*/
    private double distance =0;
    private double avgSpeed=0;
    private double avgTime;
    private Node prev=null;
    private boolean onPath=false;
    private ArrayList<Node> prevList = new ArrayList<Node>();
    private boolean visited=false;
    
    /*pozycja na ekranie*/
    private int x;
    private int y;

    public Node(){
       this.id = 0;
        connections = new ArrayList<Edge>();
        events = new ArrayList<NodeEvent>();
    }

    public void setX(int value){
        x=value;
    }

    public void setY(int value){
        y=value;
    }

    public int getX(){
        return x;
    }

    public int getY(){
        return y;
    }

    public Node(int id){
        this.id = id;
        connections = new ArrayList<Edge>();
        events = new ArrayList<NodeEvent>();
    }

    public Node(int id,String s){
        this.id = id;
        this.name=s;
        connections = new ArrayList<Edge>();
        events = new ArrayList<NodeEvent>();
    }

    public int getId(){
        return id;
    }

    public void setId(int value){
        id = value;
    }

    public String getName(){
        return name;
    }

    public void setName(String s){
        name = s;
    }

    public void addEdge(Edge e){
        connections.add(e);
    }

    public void removeEdge(Edge e){
       connections.remove(e);
    }

    public Edge getEdge(String name){

        for(int i=0;i<this.connections.size();i++){
            if(connections.get(i).getName() == name){
                return connections.get(i);
            }
        }
        return null;
    }

    public ArrayList<Edge> getEdges(){
        return connections;
    }

    public void getEdge(int id){

    }

    public boolean isLock(){
        return lock;
    }

    public void lock(){
        this.lock = true;
    }

    public void unlock(){
        this.lock = false;
    }

    public void setLock(boolean lock){
        this.lock=lock;
    }
    public void addEvent(NodeEvent event){
        this.events.add(event);
    }

     public void setPrev(Node n){
        prev = n;
    }

    public void setDistance(double d){
        distance = d;
    }

    public void setOnPath(boolean v){
        onPath = v;
    }

    public double getDistance(){
        return distance;
    }

    public boolean getOnPath(){
        return onPath;
    }

    public Node getPrev(){
        return prev;
    }

    @Override
    public String toString(){
        String res = new String();
        res+=id+" "+name+"\n\t";
        for(int i=0;i<this.connections.size();i++){
            res+=connections.get(i).toString()+",";
        }
        return res;
    }

    @Override
    public boolean equals(Object o){
        if(o instanceof Node){
            Node n = (Node)o;
     //       System.out.println("Porownuje "+this.name + "["+this.id+"]"+" i "+n.name+"["+n.id+"] "+this.name.equals(n.name) +" "+ (this.id==n.id));
            if((this.name.equals(n.name)) && (this.id==n.id)){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 11 * hash + this.id;
        hash = 11 * hash + (this.name != null ? this.name.hashCode() : 0);
        return hash;
    }



    public int compareTo(Node o) {
        double distb = ((Node)o).getDistance();
        return (distance == distb)? 0 : (distance > distb)? 1:-1;
    }

    public Color getColor() {
        return this.color;
    }

    public void setColor(Color c){
        color = c;
    }

    public Color getDefColor() {
        return this.defcolor;
    }

    public void setDefColor(Color c){
        defcolor = c;
    }

    public void clearPrev(){
        this.prevList.clear();
        prev=null;
        visited=false;
        avgSpeed=0;
        avgTime=Double.MAX_VALUE;
        distance=Double.MAX_VALUE;
    }

    public void setVisited(boolean value){
        visited=value;
    }

    public boolean getVisited(){
        return visited;
    }

    public double getAvgSpeed(){
        return avgSpeed;
    }

    public double getAvgTime(){
        return avgTime;
    }

    public void setAvgTime(double t){
        avgTime = t;
    }

    public void setAvgSpeed(double s){
        avgSpeed = s;
    }


}
